//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiPackage;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class BeetlSqlConst {
  public String BEETL_SQL_PATH = "sql/";
  public String DELIMITER_PLACEHOLDER_START = "#{";
  public String DELIMITER_PLACEHOLDER_END = "}";
  public String DELIMITER_STATEMENT_START = "-- @";
  public String DELIMITER_STATEMENT_END = "";
  public String DELIMITER_PLACEHOLDER_START2 = "${";
  public String DELIMITER_PLACEHOLDER_END2 = "}";
  public String BASE_MAPPER_BEETL_CLASS = "org.beetl.sql.core.mapper.BaseMapper";
  public String BASE_MAPPER_BEETL_PACKAGE = "org.beetl.sql.core.mapper";
  public String SQL_RESOURCE_BEETL_CLASS = "org.beetl.sql.core.annotatoin.SqlResource";
  public String PARAM_BEETL_CLASS = "org.beetl.sql.core.annotatoin.Param";
  public String PAGE_QUERY_BEETL_CLASS = "org.beetl.sql.core.engine.PageQuery";
  public String PAGE_REQUEST_BEETL_CLASS = "org.beetl.sql.core.page.PageRequest";
  public String ROOT_BEETL_CLASS = "org.beetl.sql.mapper.annotation.Root";

  public PsiClass BASE_MAPPER_PSI_CLASS = null;
  public VirtualFile defaultPropertiesFile;
  public VirtualFile extPropertiesFile;

  public BeetlSqlConst() {
  }

  public BeetlSqlConst(Project project) {
    this.handle(project);
  }

  protected void handle(Project project) {
    if (project != null) {
      JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
      PsiPackage psiPackage = javaPsiFacade.findPackage(this.BASE_MAPPER_BEETL_PACKAGE);
      if (psiPackage != null) {
        for(PsiClass psiClass : psiPackage.getClasses()) {
          if (StringUtil.equals(psiClass.getQualifiedName(), this.BASE_MAPPER_BEETL_CLASS)) {
            this.BASE_MAPPER_PSI_CLASS = psiClass;
            break;
          }
        }

        this.defaultPropertiesFile = (VirtualFile)FilenameIndex.getVirtualFilesByName("btsql.properties", true, GlobalSearchScope.allScope(project)).stream().findFirst().orElse(null);
        this.handleProperties(this.defaultPropertiesFile);
        this.extPropertiesFile = (VirtualFile)FilenameIndex.getVirtualFilesByName("btsql-ext.properties", true, GlobalSearchScope.allScope(project)).stream().findFirst().orElse(null);
        this.handleProperties(this.extPropertiesFile);
      }
    }
  }

  private void handleProperties(VirtualFile propertiesFile) {
    if (propertiesFile != null) {
      try {
        InputStream inputStream = propertiesFile.getInputStream();

        try {
          Properties properties = new Properties();
          properties.load(inputStream);
          this.DELIMITER_STATEMENT_START = properties.getProperty("DELIMITER_STATEMENT_START", this.DELIMITER_STATEMENT_START);
          this.DELIMITER_STATEMENT_END = properties.getProperty("DELIMITER_STATEMENT_END", this.DELIMITER_STATEMENT_END);
          this.DELIMITER_PLACEHOLDER_START = properties.getProperty("DELIMITER_PLACEHOLDER_START", this.DELIMITER_PLACEHOLDER_START);
          this.DELIMITER_PLACEHOLDER_END = properties.getProperty("DELIMITER_PLACEHOLDER_END", this.DELIMITER_PLACEHOLDER_END);
          this.DELIMITER_PLACEHOLDER_START2 = properties.getProperty("DELIMITER_PLACEHOLDER_START2", this.DELIMITER_PLACEHOLDER_START2);
          this.DELIMITER_PLACEHOLDER_END2 = properties.getProperty("DELIMITER_PLACEHOLDER_END2", this.DELIMITER_PLACEHOLDER_END2);
        } catch (Throwable var6) {
          if (inputStream != null) {
            try {
              inputStream.close();
            } catch (Throwable var5) {
              var6.addSuppressed(var5);
            }
          }

          throw var6;
        }

        if (inputStream != null) {
          inputStream.close();
        }

      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }
}
